new Vue({
	el: '#app',
	data: function () {
		return {
			host: window.location.protocol+'//'+window.location.host,
            communityId:'',
            alertList:[
                // {title:'养老设备预警',name:'门磁',mobile:'15125145252',room:'2-12-1202',time:'2021-07-14 15:25:20',type:'黄色'},
                // {title:'养老设备预警',name:'紧急按钮',mobile:'15125145252',room:'2-12-1202',time:'2021-07-15 15:25:20',type:'红色'}
            ],
            list_length:0,
            finished_text:'没有更多了',
            loading: false,
            finished: false,
            refreshing: false,
            totalPage:1,
            pageIndex:0,
            id:'',
            requestHeaders:'',
		}
	},
	created() {
        var that=this;
        var localStorageCommunityId=JSON.parse(localStorage.getItem('hc_currentCommunityInfo'))
		if(localStorageCommunityId){
			this.communityId=localStorageCommunityId.communityId?localStorageCommunityId.communityId:this.getUrlParam("communityId");
		}else{
			this.communityId=this.getUrlParam("communityId");
        }   
        that.requestHeaders=getRequestHeaders('00',this.communityId);//获取请求头
        // console.log(that.requestHeaders)
    },

	filters: {
		subStringThree: function (value) {
			if (!value) return ''
			return value.substring(0,3)
		}
	},
	methods: {
		getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg); //匹配目标参数
			if (r != null) return unescape(r[2]);
			return null; //返回参数值	  
		},
        //去处理
        handleClick(id,bizId,taskObjType){
            window.location.href='/agencyTaskDetail.html?id='+id+'&bizId='+bizId;
        },
        onDownRefresh() {
            this.pageIndex++;
            this.finished = false // 不写这句会导致你上拉到底过后在下拉刷新将不能触发下拉加载事件
            this.loadList() // 加载数据
        },
        onRefresh() {
            // // 清空列表数据
            this.finished = false;
            if(this.list_length<=0){
                this.refreshing=false;
            }
            // // 重新加载数据
            // // 将 loading 设置为 true，表示处于加载状态
            this.loading = true;
            var that=this;
            this.pageIndex=1;
            setTimeout(function(){
                that.loadList();
            },1000)
        },
        loadList(){
            var that=this;
            var url=this.host+'/app/task.listTasksNew';
            var params={
                page:this.pageIndex,
                row:10,
            }
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='0000'){
                        that.totalPage=res.records;
                        var dataList=res.data;
                        that.list_length=dataList.length;
                        // console.log(163,dataList)
                        if(that.list_length<1){
                            that.finished_text=''
                        }else{
                            that.finished_text='没有更多了'
                        }
                        if( dataList==null || dataList=='' || dataList.length == 0){
                            // 加载结束
                            that.refreshing=false
                            that.loading = false;
                            that.finished = true;
                            return
                        }
                        if (that.refreshing) {
                            that.alertList = [];
                            that.refreshing = false;
                        }
                         if (params.page === 1) {
                            that.alertList = dataList
                        } else {
                            that.alertList = that.alertList.concat(dataList)
                        }
                        that.loading = false;
                        if (params.page == that.totalPage) {
                            that.finished = true;
                        }
					}else{
                        that.refreshing=false
                        that.loading = false;
                        that.finished_text='';
                        that.finished = true;
                        that.showLoading=false;
					}
				},
				error:function(res){
                    that.refreshing=false
                    that.loading = false;
                    that.finished_text='';
                    that.finished = true;
                    that.showLoading=false;
				}
			})
        },

	}
});
